import Combine

//
//  Server.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/6.
//
import Foundation
import SwiftUI
import TLCLock

struct Server {
    
    // mac
    static var testMac: String = {
        let code = UserDefaults.standard.string(forKey: "TEST_MAC") ?? ""
        if !code.isEmpty {
            return code
        }
        return "ED:44:C9:81:AB:05"
    }()
    {
        didSet {
            UserDefaults.standard.set(testMac, forKey: "TEST_MAC")
        }
    }
    
    // lockKey
    static var testLockkey: String = {
        let code = UserDefaults.standard.string(forKey: "TEST_LOCK_KEY") ?? ""
        if !code.isEmpty {
            return code
        }
        return "51fb984199c75b7c78d91d443440c843"
    }()
    {
        didSet {
            UserDefaults.standard.set(testLockkey, forKey: "TEST_LOCK_KEY")
        }
    }

    // featureCode
    static var testFeatureCode: String = {
        let code = UserDefaults.standard.string(forKey: "TEST_FEATURE_CODE") ?? ""
        if !code.isEmpty {
            return code
        }
        // return "68348E13D7C5451A11A2F9A350399BDB"
        return ""
    }()
    {
        didSet {
            UserDefaults.standard.set(testFeatureCode, forKey: "TEST_FEATURE_CODE")
        }
    }
    
    // trackId
    static var testTrackId: UInt32? = {
        let code = UserDefaults.standard.string(forKey: "TEST_TRACK_ID") ?? ""
        if !code.isEmpty {
            return UInt32(code)
        }
        return nil
    }()
    {
        didSet {
            if let trackId = testTrackId {
                UserDefaults.standard.set("\(trackId)", forKey: "TEST_TRACK_ID")
            } else {
                UserDefaults.standard.removeObject(forKey: "TEST_TRACK_ID")
            }
        }
    }

    static func now() -> Date {
        return Date()
    }
    
    static func getSecret(_ mac: String) -> String? {
        if mac == testMac {
            let secret = testLockkey
            return secret.count == 32 ? secret : nil
        }
        
        return nil
    }
}

//extension Server {
//    private static let macKey = "TestLock_mac"
//
//    static func updateTestLockInfo(mac: String, secret: String, featureCode: String?) {
//        testLock = TLCManager.getLockDevice(mac: mac, featureCode: featureCode)
//        saveSecret(secret, for: mac)
//    }
//
//    static let testLockSecret = "51fb984199c75b7c78d91d443440c843"
//    private(set) static var testLock: LockDevice = {
//        if let mac = UserDefaults.standard.value(forKey: macKey) as? String, !mac.isEmpty {
//            // let secret = getSecret(mac)
//            return TLCManager.getLockDevice(mac: mac)
//        }
//
//        return TLCManager.getLockDevice(mac: "ED:44:C9:81:AB:05")
//    }() {
//        didSet {
//            UserDefaults.standard.setValue(testLock.macAddress, forKey: macKey)
//        }
//    }
//}


// rom
extension Server {
    private static var romData: (String, Data)?
    static var romUrl: String {
        return rom_2821.url
    }

    static var romMd5Str: String {
        return rom_2821.md5
    }
    
    private static let rom_2821 = RomDataModel(
        url: "https://static.yuxiaor.com/invalid_files/cams/tlc_2821.bin",
        md5: "92dc89a3a3c0758479d0406977e0d40a"
    )

    static func getRomData(_ romUrl: String) async throws -> Data? {
        if let romData = Server.romData, romData.0 == romUrl {
            return romData.1
        }
        guard let url = URL(string: romUrl) else {
            return nil
        }

        return try await withCheckedThrowingContinuation { c in
            DispatchQueue.global().async {
                do {
                    let data = try Data(contentsOf: url)
                    self.romData = (romUrl, data)
                    c.resume(returning: data)
                } catch {
                    c.resume(throwing: error)
                }
            }
        }
    }
}

struct RomDataModel {
    let url: String
    let md5: String
}
